package com.tanhua.autoconfig.properties;

import com.aliyun.imageaudit20191230.Client;
import com.aliyun.imageaudit20191230.models.ScanImageRequest;
import com.aliyun.imageaudit20191230.models.ScanTextRequest;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
@ConfigurationProperties(prefix = "tanhua.audit")
public class GreenProperties {
    /**
     * 账号
     */
    private String accessKey;
    /**
     * 密钥
     */
    private String secret;

    /**
     * 场景
     */
    private String textScenes;

    private String imageScenes;

    private String endpoint;

    @Bean
    public Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKey)
                // 您的AccessKey Secret
                .setAccessKeySecret(secret);
                //.setRegionId("cn-shanghai");

        // 访问的域名
        config.endpoint = endpoint;
        return new Client(config);
    }

    @Bean
    public ScanTextRequest getScanTextRequest() {
        String[] split = textScenes.split(",");
        List<ScanTextRequest.ScanTextRequestLabels> labels = new ArrayList<>();
        for (String s : split) {
            ScanTextRequest.ScanTextRequestLabels label = new ScanTextRequest.ScanTextRequestLabels()
                    .setLabel(s);
            labels.add(label);
        }
        return new ScanTextRequest().setLabels(labels);
    }

    @Bean
    public ScanImageRequest getScanImageRequest() {
        String[] split = imageScenes.split(",");
        return new ScanImageRequest().setScene(Arrays.asList(split));
    }
}